home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / string / nsStringBuffer.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  7KB  |  171 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla.
  17.  *
  18.  * The Initial Developer of the Original Code is IBM Corporation.
  19.  * Portions created by IBM Corporation are Copyright (C) 2003
  20.  * IBM Corporation. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Darin Fisher <darin@meer.net>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. #ifndef nsStringBuffer_h__
  40. #define nsStringBuffer_h__
  41.  
  42. /**
  43.  * This structure precedes the string buffers "we" allocate.  It may be the
  44.  * case that nsTAString::mData does not point to one of these special
  45.  * buffers.  The mFlags member variable distinguishes the buffer type.
  46.  *
  47.  * When this header is in use, it enables reference counting, and capacity
  48.  * tracking.  NOTE: A string buffer can be modified only if its reference
  49.  * count is 1.
  50.  */
  51. class nsStringBuffer
  52.   {
  53.     private:
  54.  
  55.       PRInt32  mRefCount;
  56.       PRUint32 mStorageSize;
  57.  
  58.     public:
  59.       
  60.       /**
  61.        * Allocates a new string buffer, with given size in bytes and a
  62.        * reference count of one.  When the string buffer is no longer needed,
  63.        * it should be released via Release.
  64.        *
  65.        * It is up to the caller to set the bytes corresponding to the string
  66.        * buffer by calling the Data method to fetch the raw data pointer.  Care
  67.        * must be taken to properly null terminate the character array.  The
  68.        * storage size can be greater than the length of the actual string
  69.        * (i.e., it is not required that the null terminator appear in the last
  70.        * storage unit of the string buffer's data).
  71.        *
  72.        * @return new string buffer or null if out of memory.
  73.        */
  74.       NS_COM static nsStringBuffer* Alloc(size_t storageSize);
  75.  
  76.       /**
  77.        * Resizes the given string buffer to the specified storage size.  This
  78.        * method must not be called on a readonly string buffer.  Use this API
  79.        * carefully!!
  80.        *
  81.        * This method behaves like the ANSI-C realloc function.  (i.e., If the
  82.        * allocation fails, null will be returned and the given string buffer
  83.        * will remain unmodified.)
  84.        *
  85.        * @see IsReadonly
  86.        */
  87.       NS_COM static nsStringBuffer* Realloc(nsStringBuffer* buf, size_t storageSize);
  88.  
  89.       /**
  90.        * Increment the reference count on this string buffer.
  91.        */
  92.       NS_COM void NS_FASTCALL AddRef();
  93.  
  94.       /**
  95.        * Decrement the reference count on this string buffer.  The string
  96.        * buffer will be destroyed when its reference count reaches zero.
  97.        */
  98.       NS_COM void NS_FASTCALL Release();
  99.  
  100.       /**
  101.        * This method returns the string buffer corresponding to the given data
  102.        * pointer.  The data pointer must have been returned previously by a
  103.        * call to the nsStringBuffer::Data method.
  104.        */
  105.       static nsStringBuffer* FromData(void* data)
  106.         {
  107.           return (nsStringBuffer*) ( ((char*) data) - sizeof(nsStringBuffer) );
  108.         }
  109.  
  110.       /**
  111.        * This method returns the data pointer for this string buffer.
  112.        */
  113.       void* Data() const
  114.         {
  115.           return (void*) ( ((char*) this) + sizeof(nsStringBuffer) );
  116.         }
  117.  
  118.       /**
  119.        * This function returns the storage size of a string buffer in bytes.
  120.        * This value is the same value that was originally passed to Alloc (or
  121.        * Realloc).
  122.        */
  123.       PRUint32 StorageSize() const
  124.         {
  125.           return mStorageSize;
  126.         }
  127.  
  128.       /**
  129.        * If this method returns false, then the caller can be sure that their
  130.        * reference to the string buffer is the only reference to the string
  131.        * buffer, and therefore it has exclusive access to the string buffer and
  132.        * associated data.  However, if this function returns true, then other
  133.        * consumers may rely on the data in this buffer being immutable and
  134.        * other threads may access this buffer simultaneously.
  135.        */
  136.       PRBool IsReadonly() const
  137.         {
  138.           return mRefCount > 1;
  139.         }
  140.  
  141.       /**
  142.        * The FromString methods return a string buffer for the given string 
  143.        * object or null if the string object does not have a string buffer.
  144.        * The reference count of the string buffer is NOT incremented by these
  145.        * methods.  If the caller wishes to hold onto the returned value, then
  146.        * the returned string buffer must have its reference count incremented
  147.        * via a call to the AddRef method.
  148.        */
  149.       NS_COM static nsStringBuffer* FromString(const nsAString &str);
  150.       NS_COM static nsStringBuffer* FromString(const nsACString &str);
  151.  
  152.       /**
  153.        * The ToString methods assign this string buffer to a given string
  154.        * object.  If the string object does not support sharable string
  155.        * buffers, then its value will be set to a copy of the given string
  156.        * buffer.  Otherwise, these methods increment the reference count of the
  157.        * given string buffer.  It is important to specify the length (in
  158.        * storage units) of the string contained in the string buffer since the
  159.        * length of the string may be less than its storage size.  The string
  160.        * must have a null terminator at the offset specified by |len|.
  161.        *
  162.        * NOTE: storage size is measured in bytes even for wide strings;
  163.        *       however, string length is always measured in storage units
  164.        *       (2-byte units for wide strings).
  165.        */
  166.       NS_COM void ToString(PRUint32 len, nsAString &str);
  167.       NS_COM void ToString(PRUint32 len, nsACString &str);
  168.   };
  169.  
  170. #endif /* !defined(nsStringBuffer_h__ */
  171.